﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using SpriteGame.Helpers;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace SpriteGame.Entities.Actors
{
    class Player : Actor
    {
        private const float WEAPON_ROTATION_SPEED = 0.1f,
            WEAPON_ROTATION_MAX_PITCH = -120f, WEAPON_ROTATION_MIN_PITCH = 120f;

        private Weapon weapon;
        private float weaponRotation;

        public Player(Texture2D _texture)
            : base(_texture)
        {
            weapon = WeaponHelper.Instance.GetWeapon(WeaponTypeEnum.Magnum);
            weaponRotation = 90f;

            position = new Vector2(200, 200);
        }

        public override void Update(GameTime _gameTime)
        {
            weapon.Update();

            if (InputHandler.IsGamepadButtonPressed(Microsoft.Xna.Framework.Input.Buttons.RightTrigger))
                weapon.Shoot(_gameTime);

            checkPitch();
            checkWeaponChange();

            base.Update(_gameTime);
        }

        public override void Draw(SpriteBatch _spriteBatch)
        {
            _spriteBatch.Draw(texture, position, Color.White);

            weapon.Draw(_spriteBatch, direction, position, weaponRotation);

            base.Draw(_spriteBatch);
        }

        private void checkPitch()
        {
            Vector2 thumbstickVector = InputHandler.GetThumbstickVector(false);
            if (!(thumbstickVector.X == 0) && !(thumbstickVector.Y == 0))
            {
                weaponRotation = (float)Math.Atan2(thumbstickVector.X, thumbstickVector.Y);
                weaponRotation = MathHelper.Clamp(weaponRotation, WEAPON_ROTATION_MAX_PITCH, WEAPON_ROTATION_MIN_PITCH);

                if (weaponRotation >= 0)
                    direction = DirectionEnum.Forward;
                else
                    direction = DirectionEnum.Backward;
            }
        }

        private void checkWeaponChange()
        {
            if (InputHandler.IsGamepadButtonPressed(Microsoft.Xna.Framework.Input.Buttons.RightShoulder))
            {
                nextWeapon();
                return;
            }

            if (InputHandler.IsGamepadButtonPressed(Microsoft.Xna.Framework.Input.Buttons.LeftShoulder))
            {
                previousWeapon();
                return;
            }

            GamePadDPad gamePadState = InputHandler.GetDPadState();

            if (gamePadState.Up == ButtonState.Pressed && gamePadState.Right == ButtonState.Pressed)
            {
                weapon = WeaponHelper.Instance.GetWeapon((WeaponTypeEnum)1);
                return;
            }

            if (gamePadState.Up == ButtonState.Pressed && gamePadState.Left == ButtonState.Pressed)
            {
                weapon = WeaponHelper.Instance.GetWeapon((WeaponTypeEnum)7);
                return;
            }

            if (gamePadState.Down == ButtonState.Pressed && gamePadState.Right == ButtonState.Pressed)
            {
                weapon = WeaponHelper.Instance.GetWeapon((WeaponTypeEnum)3);
                return;
            }

            if (gamePadState.Down == ButtonState.Pressed && gamePadState.Left == ButtonState.Pressed)
            {
                weapon = WeaponHelper.Instance.GetWeapon((WeaponTypeEnum)5);
                return;
            }

            if (gamePadState.Up == ButtonState.Pressed)
            {
                weapon = WeaponHelper.Instance.GetWeapon((WeaponTypeEnum)0);
                return;
            }

            if (gamePadState.Down == ButtonState.Pressed)
            {
                weapon = WeaponHelper.Instance.GetWeapon((WeaponTypeEnum)4);
                return;
            }

            if (gamePadState.Left == ButtonState.Pressed)
            {
                weapon = WeaponHelper.Instance.GetWeapon((WeaponTypeEnum)6);
                return;
            }

            if (gamePadState.Right == ButtonState.Pressed)
            {
                weapon = WeaponHelper.Instance.GetWeapon((WeaponTypeEnum)2);
                return;
            }
        }

        private void nextWeapon()
        {
            Int32 nextWeapon = (Int32)weapon.WeaponType + 1;

            if (nextWeapon > Enum.GetValues(typeof(WeaponTypeEnum)).Length - 1)
                nextWeapon = 0;

            weapon = WeaponHelper.Instance.GetWeapon((WeaponTypeEnum)nextWeapon);
        }

        private void previousWeapon()
        {
            Int32 previousWeapon = (Int32)weapon.WeaponType - 1;

            if (previousWeapon < 0)
                previousWeapon = Enum.GetValues(typeof(WeaponTypeEnum)).Length - 1;

            weapon = WeaponHelper.Instance.GetWeapon((WeaponTypeEnum)previousWeapon);
        }
    }

}
